package com.youngwang.lambda;

import com.google.common.collect.Lists;
import com.youngwang.lambda.infa.PersonFactory;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LambdaTest {

    public static void main(String[] args) {
        // Person:;new 语法需要@FunctionalInterface注解标明，或者只有一个抽象方法
        PersonFactory<Person> factory = Person::new;

        Person[] persons = {factory.create("Nie", "Chao", 18), factory.create("Lin", "XiaoHui", 25)};
        List<Person> pList = Lists.asList(factory.create("Wang", "Yang", 15), persons);

        Stream<Person> students = pList.stream().filter(p -> p.getAge() > 18).map(Function.identity());

        students.forEach(System.out::println);
        System.out.println("----------------------");
        List<Person> per = pList.stream().filter(p -> p.getAge() < 18).map(Function.identity()).collect(Collectors.toList());

        per.forEach(s -> {
            System.out.print(s.getFirstName());
            System.out.println(s.getLastName());
            System.out.println(s.getAge());
        });
        System.out.println("----------------------");
        int[] test = {1, 2, 3, 4, 5, 6, 7};
        IntStream.of(test).forEach(System.out::println);
        IntStream.range(1, 3).forEach(System.out::println);

        List<String> strList = Lists.newArrayList("wangyang", "niechao");
        String[] strArray = strList.stream().toArray(String[]::new);
        for (String s : strArray) {
            System.out.println(s);
        }
    }
}
